/*
 * Copyright (C) 2012 BARBOTIN Nicolas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GCEADialog.h"
#include "GCUtils.h"

GCEADialog::GCEADialog(GCDataReader *d, bool isEvent, QWidget *parent) :
    QDialog(parent)
{
    dr = d;
    type = isEvent;

    layout = new QVBoxLayout();
    filter = new QComboBox();
    list = new QListWidget();
    buttons = new QDialogButtonBox();

    buttons->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    filter->addItem(tr("Tous"), QVariant(-1));

    if(isEvent) {
        for(int i = 0; i < dr->countEventCats(); i++)
            filter->addItem(QIcon(dr->getCategory(dr->getEventCatIndex(i)).icon), dr->getCategory(dr->getEventCatIndex(i)).name, dr->getEventCatIndex(i));

        for(int i = 0; i < dr->countEvents(); i++) {
            QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByCat(dr->getEvent(i).eventCategory, dr), dr->getEvent(i).eventName);
            cItem->setData(Qt::UserRole, QVariant(i));
            list->addItem(cItem);
        }
    } else {
        for(int i = 0; i < dr->countActionCats(); i++)
            filter->addItem(QIcon(dr->getACategory(dr->getActionCatIndex(i)).icon), dr->getACategory(dr->getActionCatIndex(i)).name, dr->getActionCatIndex(i));

        for(int i = 0; i < dr->countActions(); i++) {
            QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByACat(dr->getAction(i).actionCategory, dr), dr->getAction(i).actionName);
            cItem->setData(Qt::UserRole, QVariant(i));
            list->addItem(cItem);
        }
    }

    connect(filter, SIGNAL(currentIndexChanged(int)), this, SLOT(changeFilter(int)));
    connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
    connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(accept()));

    layout->addWidget(filter);
    layout->addWidget(list);
    layout->addWidget(buttons);
    setLayout(layout);

    setWindowTitle(tr("GameGenerator"));
    setWindowIcon(QIcon(":/icon.png"));
}

int GCEADialog::getClicked()
{
    if(list->currentRow() == -1)
        return -1;

    return list->currentItem()->data(Qt::UserRole).toInt();
}

void GCEADialog::changeFilter(int index)
{
    int cat = filter->itemData(index, Qt::UserRole).toInt();
    list->clear();

    if(cat == -1) {
        if(type) {
            for(int i = 0; i < dr->countEvents(); i++) {
                QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByCat(dr->getEvent(i).eventCategory, dr), dr->getEvent(i).eventName);
                cItem->setData(Qt::UserRole, QVariant(i));
                list->addItem(cItem);
            }
        } else {
            for(int i = 0; i < dr->countActions(); i++) {
                QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByACat(dr->getAction(i).actionCategory, dr), dr->getAction(i).actionName);
                cItem->setData(Qt::UserRole, QVariant(i));
                list->addItem(cItem);
            }
        }
        return;
    }

    if(type) {
        for(int i = 0; i < dr->countEvents(); i++) {
            if(dr->getEvent(i).eventCategory == cat) {
                QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByCat(dr->getEvent(i).eventCategory, dr), dr->getEvent(i).eventName);
                cItem->setData(Qt::UserRole, QVariant(i));
                list->addItem(cItem);
            }
        }
    } else {
        for(int i = 0; i < dr->countActions(); i++) {
            if(dr->getAction(i).actionCategory == cat) {
                QListWidgetItem *cItem = new QListWidgetItem(GCUtils::getIconByACat(dr->getAction(i).actionCategory, dr), dr->getAction(i).actionName);
                cItem->setData(Qt::UserRole, QVariant(i));
                list->addItem(cItem);
            }
        }
    }
}
